var express = require('express');
var crypto = require('crypto');
var resp = require('../config/responseJson');
var User = require('../model/user');
var Post = require('../model/post');
var router = express.Router();

/* GET users listing. */
router.get('/reg', function(req, res, next) {
    let option = {
        user: req.session.user, //不显示登陆form
        isIndexRouter: false,
        isActivePostLi: ''
    };
    res.render('regPage', option);
});

router.post('/reg', function(req, res, next) {
    //生成口令的散列值
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');
    let body = req.body;
    if (!body.password || !body.passwordRepeat || !body.name) {
        resp.error.msg = '请填写必填字段';
        return res.json(resp.error);
    }
    if (body.passwordRepeat !== body.password) {
        resp.error.msg = '两次密码不相同';
        return res.json(resp.error);
    }
    User.findOne({ name: body.name })
        .then(data => {
            if (!data) {
                let user = new User();
                user.name = body.name;
                user.password = password;
                user.save()
                    .then(data => {
                        req.session.user = data;
                        res.json(resp.success);
                    })
                    .catch(err => {
                        next(err);
                    });
            } else {
                resp.error.msg = '该用户名已存在';
                res.json(resp.error);
            }
        })
        .catch(err => {
            next(err);
        });
});

router.post('/login', function(req, res, next) {
    //生成口令的散列值
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');
    let body = req.body;
    let option = {
        isIndexRouter: true,
        user: req.session.user,
        isActivePostLi: ''
    };
    if (!body.password || !body.name) {
        resp.error.msg = '请填写用户名和密码';
        option.resp = resp.error;
        Post.find({})
            .then(data => {
                option.posts = data;
                option.rowNum = Math.ceil(data.length / 3);
                res.render('layoutPage', option);
            })
            .catch(err => next(err));
    }
    User.findOne({ name: body.name, password: password })
        .then(data => {
            let resultResp = null;
            if (!data) {
                resp.error.msg = '用户名或者密码错误';
                resultResp = resp.error;
            } else {
                req.session.user = data;
                option.user = req.session.user; //根据user是否存在于session中，显示navForm
                resp.success.msg = '登陆成功';
                resultResp = resp.success;
            }
            option.resp = resultResp;
            return Post.find({});
        })
        .then(data => {
            option.posts = data;
            option.rowNum = Math.ceil(data.length / 3);
            res.render('layoutPage', option);
        })
        .catch(err => {
            next(err);
        });
});

router.get('/logout', function(req, res, next) {
    req.session.user = null;
    res.redirect('/');
});
module.exports = router;
